package com.bing.task.v1;

import com.bing.entity.Account;
import com.bing.services.AccountServices;
import com.bing.services.NoteServices;
import com.bing.services.QuestionServices;
import com.bing.services.ReviewServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

/**
 * Created by kaibb on 15-11-23.
 */
public class UpdateAccountDb implements Runnable {
    private AccountResult accountResult;
    private QuestionServices questionServices;
    private AccountServices accountServices;
    private NoteServices noteServices;
    private ReviewServices reviewServices;
    private static final Logger LOGGER = LoggerFactory.getLogger(UpdateAccountDb.class);

    public UpdateAccountDb(AccountResult accountResult, QuestionServices questionServices, AccountServices accountServices, NoteServices noteServices, ReviewServices reviewServices) {

        this.accountResult = accountResult;
        this.questionServices = questionServices;
        this.accountServices = accountServices;
        this.noteServices = noteServices;
        this.reviewServices = reviewServices;
    }

    @Override
    public void run() {
        if (accountResult == null) {
            return;
        }
        LOGGER.info("update DB: account:{},notes:{},reviews:{}", accountResult.getAccounts().size(), accountResult.getNotes().size(), accountResult.getReviews().size());
        accountServices.updateAccounts(new ArrayList<Account>(accountResult.getAccounts()));
       // questionServices.updateIds(accountResult.getQuestions());
        reviewServices.saveReviews(accountResult.getReviews());
        noteServices.saveNotes(accountResult.getNotes());
    }
}
